<?php

namespace app\api\controller;

use app\admin\model\halt\Cate;
use app\common\controller\Api;
use app\common\model\Agent;
use app\admin\model\Githouse;


/**
 * 基础数据接口
 */
class Base extends Api
{

    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

	public function _initialize(){
        parent::_initialize();
		
	}

    /**
     * 获取费用项目
     * @author gzy
     * @date   2018年8月31日 09:10:21
     */
    public function cost() {
		$model = new \app\admin\model\base\Cost;
		 //当前是否为关联查询
        $this->relationSearch = false;
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()){
        
           return $this->selectpage($model);
            
        }
		return false;
    }
    /**
     * 获取费用项目
     * @author gzy
     * @date   2018年8月31日 09:10:21
     */
    public function lecturer() {
		$model = new \app\admin\model\base\Lecturer;
		 //当前是否为关联查询
        $this->relationSearch = false;
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()){
             return $this->selectpage($model);
        }
		return false;
    }
    /**
     * 获取场次信息
     * @author gzy
     * @date   2018年8月31日 09:10:21
     */
    public function season() {
		$model = new \app\admin\model\finance\extract\Season;
		 //当前是否为关联查询
        $this->relationSearch = false;
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()){
             return $this->selectpage($model);
        }
		return false;
    }

    /**
     * 获取学校信息
     * @author gzy
     * @date   2018年8月31日 09:10:21
     */
    public function school() {
		$model = new \app\admin\model\School;
		 //当前是否为关联查询
        $this->relationSearch = false;
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()){
             return $this->selectpage($model);
        }
		return false;
    }
    /**
     * 获取成员信息
     * @author gzy
     * @date   2018年9月3日 17:00:21
     */
    public function user() {
		$model = new \app\admin\model\Admin;
		 //当前是否为关联查询
        $this->relationSearch = false;
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()){
             return $this->selectpage($model,1);
        }
		return false;
    }

    public function getAgent() {
        $model = new Agent();
        //当前是否为关联查询
        $this->relationSearch = false;
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()){
            return $this->selectpage($model);
        }
        return false;
    }
    public function githouse() {
        $model = new Githouse();
        //当前是否为关联查询
        $this->relationSearch = false;
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()){
            return $this->selectpage($model);
        }
        return false;
    }
	 /**
     * 获取职位信息
     * @author gzy
     * @date   2018年9月3日 17:00:21
     */
    public function position() {

		  $userinfo = $this->request->post("userid");
		  if(empty($userinfo)) $this->error(GetErrorLang('-1'));
		  $userinfo = explode('-',$userinfo);
		  if(!isset($userinfo[1])) $this->error(GetErrorLang('-1'));
		  $userid = $userinfo[1];
		 
		  if ($this->request->isPost()) {
				$arr = TableListSqlQuery('Admin',"admin.userid = '".$userid."'",'userid');
				
				if(empty($arr[0]['position'])) $this->error(GetErrorLang('-1'));
				$this->success('成功',explode(',',$arr[0]['position']));
		  }
		return $this->error(GetErrorLang('-1'));
    }
    /**
     * 获取职位信息
     * @author gzy
     * @date   2018年9月3日 17:00:21
     */
    public function position_new() {
          $userid = $this->request->post("userid");
          if(empty($userid)) $this->error(GetErrorLang('-1'));
          if ($this->request->isPost()) {
                $arr = TableListSqlQuery('Admin',"admin.userid = '".$userid."'",'userid');
                if(empty($arr[0]['position'])) $this->error(GetErrorLang('-1'));
                $this->success('成功',explode(',',$arr[0]['position']));
          }
        return $this->error(GetErrorLang('-1'));
    }
	 /**
     * Selectpage的实现方法
     *
     * 当前方法只是一个比较通用的搜索匹配,请按需重载此方法来编写自己的搜索逻辑,$where按自己的需求写即可
     * 这里示例了所有的参数，所以比较复杂，实现上自己实现只需简单的几行即可
     *
     */
    protected function selectpage($model = NULL,$type = NULL)
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'htmlspecialchars']);

        //搜索关键词,客户端输入以空格分开,这里接收为数组
        $word = (array)$this->request->request("q_word/a");
        //当前页
        $page = $this->request->request("pageNumber");
        //分页大小
        $pagesize = $this->request->request("pageSize");
        //搜索条件
        $andor = $this->request->request("andOr", "and", "strtoupper");
        //排序方式
        $orderby = (array)$this->request->request("orderBy/a");
        //显示的字段
        $field = $this->request->request("showField");
        //主键
        $primarykey = $this->request->request("keyField");
        //主键值
        $primaryvalue = $this->request->request("keyValue");
        //搜索字段
        $searchfield = (array)$this->request->request("searchField/a");
        //自定义搜索条件
        $custom = (array)$this->request->request("custom/a");
		if($type == 1){
			$searchfield = array_merge($searchfield, ['position','userid','username']);
		}

        $order = [];
        foreach ($orderby as $k => $v) {
            $order[$v[0]] = $v[1];
        }
        $field = $field ? $field : 'name';

        //如果有primaryvalue,说明当前是初始化传值
        if ($primaryvalue !== null) {
            $where = [$primarykey => ['in', $primaryvalue]];
        } else {
            $where = function ($query) use ($word, $andor, $field, $searchfield, $custom,$type) {
                $logic = $andor == 'AND' ? '&' : '|';
                $searchfield = is_array($searchfield) ? implode($type == 1?'|':$logic, $searchfield) : $searchfield;
		
                foreach ($word as $k => $v) {
                    $query->where(str_replace(',', $type == 1?'|':$logic, $searchfield), "like", "%{$v}%");
                }
                if ($custom && is_array($custom)) {
                    foreach ($custom as $k => $v) {
						if($type == 1){
							$query->where($k, '<>', $v);
						}else{
						    $query->where($k, '=', $v);
						}
                    }
                }
				
            };
        }

        $list = [];
        $total = $model->where($where)->count();
        if ($total > 0) {
         
            $datalist = $model->where($where)
                ->order($order)
                ->page($page, $pagesize)
                ->select();

            foreach ($datalist as $index => $item) {
                unset($item['password'], $item['salt']);
				if($type == 1){
					$value = isset($item[$field]) ? $item[$field] : '';
					$value .=isset($item['position']) ? ' '.$item['position'] : '';
//					$value .=isset($item['userid']) ? '-'.$item['userid'] : '';
					$list[] = [
						$primarykey => isset($item[$primarykey]) ? $item[$primarykey] : '',
						$field      => $value
					];
				}else{
					$list[] = [
						$primarykey => isset($item[$primarykey]) ? $item[$primarykey] : '',
						$field      => isset($item[$field]) ? $item[$field] : ''
					];
				}
            }
        }
        //这里一定要返回有list这个字段,total是可选的,如果total<=list的数量,则会隐藏分页按钮
        return json(['list' => $list, 'total' => $total]);
    }
	

}
